import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'dart:async';

class DateTimeDemo extends StatefulWidget {
  const DateTimeDemo({super.key});

  @override
  State<DateTimeDemo> createState() => _DateTimeDemoState();
}

class _DateTimeDemoState extends State<DateTimeDemo> {
  DateTime selectDate = DateTime.now();
  TimeOfDay selectTime = const TimeOfDay(hour: 0, minute: 0 );

  _selectDate() async {
    var date = await showDatePicker(
      context: context,
      initialDate: selectDate,
      firstDate: DateTime(1900),
      lastDate: DateTime(2100),
    );

    if (date == null) return;

    setState(() {
      selectDate = date;
    });

  }

  _selectTime() async {
    final TimeOfDay time = await showTimePicker(
      context: context,
      initialTime: selectTime,
    ) as TimeOfDay;

    if (time == null) return;
    print(time.period.name);
    setState(() {
      selectTime = time;
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SliderDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              onTap: () {
                _selectDate();
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(DateFormat.yMd().format(selectDate)),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
            ),
            InkWell(
              onTap: () {
                _selectTime();
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('${selectTime.hour}:${selectTime.minute} ${selectTime.period.name}'),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}